<?php

namespace app\controller;

use think\helper\Str;
use think\exception\HttpException;
use think\facade\View;

class Error
{

    public function __call($method, $args)
    {
        // 获取默认模块
        $module_name = config('basic.default_addons');
        //检测模块是否存在
        $addons_path = root_path() . 'addons' . DIRECTORY_SEPARATOR . $module_name;
        if (!is_dir($addons_path)) {
            throw new HttpException(404, '插件 '.$module_name.' 未找到');
        }
        //控制器
        $controller = '\addons\\' . $module_name . '\controller';
        $arr = explode('.', request()->controller());
        foreach ($arr as $k => $v) {
            if ($k == count($arr) - 1) {
                $controller .= '\\' . Str::title($v);
            } else {
                $controller .= '\\' . Str::snake($v);
            }
        }
        //检测类是否存在
        if (!class_exists($controller)) {
            throw new HttpException(404, 'controller not exists:' . ltrim($controller, '\\'));
        }
        $class = new $controller();
        if (!method_exists($class, $method)) {
            throw new HttpException(404, 'method not exists:' . ltrim($controller, '\\') . '->' . $method . '()');
        }
        return invoke([$class, $method]);
    }
}
